/*
 * Created on Jun 6, 2005
 *
 */
package com.erclab.internal.xpresso.dataAccess;

import java.sql.*;
import javax.sql.*;
import java.util.*;

import com.erclab.internal.xpresso.exceptions.DataSourceException;
import com.erclab.internal.xpresso.handlers.*;
import com.erclab.internal.xpresso.repositories.Repository;
import org.apache.commons.dbutils.*;
import org.apache.log4j.Logger;


/**
 * @author Enrique Rodriguez.
 *
 */
public class MysqlDataSourceLoader implements DataSourceLoader {

	Repository r;
	private final String DS_QUERY = "SELECT * FROM datasources";

	protected static final Logger log = Logger.getLogger(MysqlDataSourceLoader.class);
	
	public MysqlDataSourceLoader(Repository r){
		this.r = r;
	}

	/* (non-Javadoc)
	 * @see com.erclab.internal.xpresso.dataAccess.DataSourceLoader#getApplicationDataSources(com.erclab.internal.xpresso.repositories.Repository)
	 */
	public HashMap getApplicationDataSources() throws DataSourceException {
		HashMap datasources = null;
		ResultSetHandler rsHandler = new DataSourceHandler();
		QueryRunner run = new QueryRunner((DataSource)r.getRepositoryDataSource());
		try {
			 datasources = (HashMap)run.query(DS_QUERY, null, rsHandler);
		}catch(SQLException ex){
			log.error("An error ocurred loading a datasource: " + ex);
			throw new  DataSourceException("An error ocurred loading a datasource: " + ex);
		}	

		return datasources;
	}

}
